<template>
  <div class="home">
    <el-form :inline="true" :model="dataForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="dataList" border style="width: 100%">
      <el-table-column prop="name" label="名称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="createDate" label="创建时间" show-overflow-tooltip header-align="center" align="center"></el-table-column>
      <el-table-column prop="creatorName" label="创建者" header-align="center" align="center"></el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="248">
        <template v-slot="scope">
          <el-button type="primary" link @click="addOrUpdateHandle(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <add-or-update ref="addOrUpdateRef" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import AddOrUpdate from './add-or-update.vue';
const dataForm = ref({
  name: '',
});
interface DataList {
  id: number;
  name: string;
  createDate: string;
  creatorName: string;
}
const dataList = ref<DataList[]>([]);
const loading = ref(false);
// 模拟接口数据
function generate() {
  const arr = [];
  for (let index = 0; index < 8; index++) {
    arr.push({
      id: index + 1,
      name: `name${index}`,
      createDate: `2025-3-${index + 1}`,
      creatorName: 'admin',
    });
  }
  return arr;
}
// 模拟接口请求
const getDataList = () => {
  loading.value = true;
  setTimeout(() => {
    dataList.value = generate();
    loading.value = false;
  }, 1500);
};
onMounted(() => {
  getDataList();
});

// 新增或者修改弹窗
const addOrUpdateRef = ref();
const addOrUpdateHandle = ({ id, name }: { id?: string; name?: string } = {}) => {
  addOrUpdateRef.value.init(id, name);
};
</script>
